package com.aizhixin.lab.post.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.project.template.domain.WorkOrderDomain;
import com.aizhixin.lab.post.entity.Business;
import com.aizhixin.lab.post.repository.BusinessRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RequestMapping("/api/v1/post/business")
@RestController
@Api(tags = "行业", value = "行业")
public class BusinessController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private BusinessRepository businessRepository;

    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "保存", httpMethod = "POST", notes = "保存<br><br><b>lwq</b>")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token, @RequestBody Business business) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        if (StringUtils.isEmpty(business.getId())) {
            business.setId(UUID.randomUUID().toString());
            business.setDeleteFlag(DataValidity.VALID.getState());
        }
        businessRepository.save(business);
        result.put(ApiReturnConstants.SUCCESS, true);
        result.put(ApiReturnConstants.MESSAGE, "操作成功");
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除行业", httpMethod = "DELETE", notes = "删除行业<br><br><b>lwq</b>")
    public ResponseEntity<?> deleteWorkOrderFile(@RequestHeader("Authorization") String token, @RequestParam String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Business business = businessRepository.findByIdAndDeleteFlag(id, DataValidity.VALID.getState());
        business.setDeleteFlag(DataValidity.INVALID.getState());
        businessRepository.save(business);
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findAllBusiness", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取所有行业", httpMethod = "GET", notes = "获取所有行业<br><br><b>lwq</b>")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = WorkOrderDomain.class),})
    public ResponseEntity<?> findWorkOrderInfo(@RequestHeader("Authorization") String token) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<Business> businessList = businessRepository.findByDeleteFlagOrderByCreatedDateDesc(DataValidity.VALID.getState());
        result.put(ApiReturnConstants.SUCCESS, true);
        result.put(ApiReturnConstants.DATA,businessList);
        return new ResponseEntity(result, HttpStatus.OK);
    }
}
